home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT65.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  5.3 KB  |  172 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 65                         
  5.                                           
  6.  Tile Animation!
  7.  
  8.  This example shows two method of animating tiles within a scrolling window.
  9.  The first technique is to reserve a tile number for the animated tile.
  10.  The pointer contained in the tile array will be changed, so each time the
  11.  screen is drawn a new image represents that tile.  If you look at the 
  12.  anim.spr file, I've set up the first animating tile in positions 1-6.
  13.  The seventh position is the tile I will animate, and is the same image
  14.  as tile 1.  This lets you draw the map using tile 7.  If you use tiles
  15.  1 to 6 on the map, they will not animate under this method.  Since tile
  16.  seven's image pointer is going to change, we have to free the original
  17.  image first.
  18.  
  19.  The second method cycles a range of image pointers.  This allows you to draw
  20.  using any tile within the range.  
  21.  
  22.  Use the arrow keys to scroll, and ESC quits.
  23.  
  24.                                           
  25.  *** PROJECT ***                                                             
  26.  This program requires the files WGT5_WC.LIB and WSCR_WC.LIB to be linked.   
  27.                                           
  28.  *** DATA FILES ***                                                          
  29.  ANIM.SPR, ANIM.WMP                                                          
  30.                                WATCOM C++ VERSION 
  31. ==============================================================================
  32. */
  33.  
  34. #include <dos.h>
  35. #include <malloc.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <conio.h>
  39. #include <wgt5.h>
  40. #include <wgtscrol.h>
  41.  
  42. #define UP 72
  43. #define DOWN 80
  44. #define LEFT 75
  45. #define RIGHT 77
  46. #define ESC 1
  47. #define WIN 0
  48.  
  49. short spx, spy;                    /* Speed of scrolling window */
  50. wgtmap animmap;                     /* our world map */
  51. color palette[256];                /* Our palette of colours */
  52. block tiles[100];                  /* Tiles for the map */
  53. block sprites[10];                 /* Our sprites */
  54. scrollsprite wobject[100];         /* Objects in window */
  55. short tiletypes[256];              /* Tile types */
  56.  
  57. short oldmode;                     /* Previous video mode */
  58.  
  59.  
  60. int tile_anim1;                    /* Current tile number in first animation */
  61.  
  62. void animate_tiles (void)
  63. {
  64. block temp;
  65. int i;
  66.  
  67.  /* First method */
  68.  /* The first animation sequence is tiles 1 to 6. */
  69.  tile_anim1++;
  70.  if (tile_anim1 > 6)               /* Repeat animation */
  71.      tile_anim1 = 1;
  72.     
  73.  tiles[7] = tiles[tile_anim1];     /* Make it point to the new image */
  74.  
  75.  
  76.  
  77.  /* Second method */
  78.  /* The second animation sequence is tiles 8 to 17. */
  79.  
  80.  temp = tiles[8];  /* Store the pointer to the first tile in the sequence */
  81.  
  82.  for (i = 8; i < 17; i++)          /* Shift all the pointers up one */
  83.   tiles[i] = tiles[i+1];
  84.  
  85.  tiles[17] = temp;                 /* Set the last pointer with the first */
  86. }
  87.  
  88.  
  89.  
  90. void main(void)
  91. {
  92.  
  93.   oldmode = wgetmode ();
  94.   if (!vgadetected ())
  95.   {
  96.     printf ("VGA is required to run this program...");
  97.     exit (1);
  98.   }
  99.  
  100.   printf ("WGT Example #65\n\n");
  101.   printf ("Tile Animation!\n");
  102.   printf ("This example shows two method of animating tiles within a scrolling window.\n");
  103.   printf ("The first technique is to reserve a tile number for the animated tile.\n");
  104.   printf ("The pointer contained in the tile array will be changed, so each time the\n");
  105.   printf ("screen is drawn a new image represents that tile.  If you look at the\n");
  106.   printf ("anim.spr file, I've set up the first animating tile in positions 1-6.\n");
  107.   printf ("The seventh position is the tile I will animate, and is the same image\n");
  108.   printf ("as tile 1.  This lets you draw the map using tile 7.  If you use tiles\n");
  109.   printf ("1 to 6 on the map, they will not animate under this method.  Since tile\n");
  110.   printf ("seven's image pointer is going to change, we have to free the original\n");
  111.   printf ("image first.\n");
  112.   printf ("Use the arrow keys to scroll and ESC quits.\n");
  113.   printf ("\nPress any key to begin.\n");
  114.   getch ();
  115.  
  116.   vga256 ();
  117.  
  118.   tile_anim1 = 1;
  119.   /* Tile image number 1 */
  120.  
  121.   /* Load the graphics */
  122.   wloadsprites (palette, "anim.spr", tiles, 0, 99);
  123.   
  124.   wfreeblock (tiles[7]);
  125.   /* Free the first animating tile */
  126.  
  127.   tiles[7] = tiles[tile_anim1];
  128.   /* Set the image to be the first tile in the animation.  To animate  
  129.      the tile, we just need to change the value of tile_anim1. */
  130.  
  131.   wsetpalette (0, 255, palette);
  132.  
  133.   winitscroll (WIN, NORMAL, -1, 10, 10, tiles);
  134.   animmap = wloadmap (WIN, "anim.wmp", tiletypes, wobject);
  135.  
  136.   wcls (0);
  137.  
  138.   wshowwindow (WIN, 0, 0);
  139.   
  140.   installkbd ();
  141.  
  142.   do {
  143.     spx = 0;
  144.     spy = 0;
  145.  
  146.     animate_tiles ();
  147.  
  148.     /* check the keyboard */
  149.     if (kbdon[RIGHT])
  150.      spx += 4; 
  151.     else if (kbdon[LEFT])
  152.      spx -= 4; 
  153.     
  154.     if (kbdon[DOWN])
  155.      spy += 4; 
  156.     if (kbdon[UP])
  157.      spy -= 4; 
  158.     
  159.     wscrollwindow (WIN, spx, spy);
  160.     
  161.     wretrace ();
  162.     wputblock (0, 0, scrollblock[0], NORMAL);
  163.   } while (!kbdon[ESC]);                 // until ESC is pressed
  164.  
  165.   uninstallkbd ();
  166.   wendscroll (WIN);
  167.   wfreesprites (tiles, 0, 99);
  168.   wfreemap (animmap);
  169.   wsetmode (oldmode);
  170. }
  171.  
  172.